﻿using SD.Toolkits.SerialNumber.Mediators;
using SlamDunk.MES.Domain.Entities.MaintenanceContext;
using SlamDunk.MES.Domain.Entities.PlanContxt;
using SlamDunk.MES.Domain.Entities.QualityContext;
using SlamDunk.MES.Domain.Entities.StockContext;
using SlamDunk.MES.Domain.IDomainServices;

namespace SlamDunk.MES.DomainService.Implements
{
    /// <summary>
    /// 编号领域服务实现
    /// </summary>
    public class NumberService : INumberService
    {
        #region # 常量、字段及构造器

        /// <summary>
        /// 默认日期时间格式
        /// </summary>
        private const string DefaultTimeFormat = "yyyyMMdd";

        /// <summary>
        /// 编号生成器
        /// </summary>
        private readonly NumberGenerator _generator;

        /// <summary>
        /// 构造器
        /// </summary>
        public NumberService()
        {
            this._generator = new NumberGenerator();
        }

        #endregion

        #region # 生成库存单据编号 —— string GenerateStockOrderNo(string prefix)
        /// <summary>
        /// 生成库存单据编号
        /// </summary>
        /// <param name="prefix">前缀</param>
        /// <returns>单据编号</returns>
        public string GenerateStockOrderNo(string prefix)
        {
            return this._generator.Generate(typeof(StockOrder).FullName, prefix, DefaultTimeFormat, 3, "库存单据编号");
        }
        #endregion

        #region # 生成销售订单编号 —— string GenerateSaleOrderNo()
        /// <summary>
        /// 生成销售订单编号
        /// </summary>
        /// <returns>销售订单编号</returns>
        public string GenerateSaleOrderNo()
        {
            return this._generator.Generate(typeof(SaleOrder).FullName, "SO", DefaultTimeFormat, 3, "库存单据编号");
        }
        #endregion

        #region # 生成生产计划编号 —— string GenerateProductionPlanNo()
        /// <summary>
        /// 生成生产计划编号
        /// </summary>
        /// <returns>生产计划编号</returns>
        public string GenerateProductionPlanNo()
        {
            return this._generator.Generate(typeof(ProductionPlan).FullName, "MPS", DefaultTimeFormat, 2, "生产计划编号");
        }
        #endregion

        #region # 生成作业计划编号 —— string GenerateExecutionPlanNo()
        /// <summary>
        /// 生成作业计划编号
        /// </summary>
        /// <returns>作业计划编号</returns>
        public string GenerateExecutionPlanNo()
        {
            return this._generator.Generate(typeof(ExecutionPlan).FullName, "EP", DefaultTimeFormat, 2, "作业计划编号");
        }
        #endregion

        #region # 生成作业计划质量检查编号 —— string GenerateExecutionPlanInspectionNo()
        /// <summary>
        /// 生成作业计划质量检查编号
        /// </summary>
        /// <returns>作业计划质量检查编号</returns>
        public string GenerateExecutionPlanInspectionNo()
        {
            return this._generator.Generate(typeof(ExecutionPlanInspection).FullName, "EPQI", DefaultTimeFormat, 2, "作业计划质量检查编号");
        }
        #endregion

        #region # 生成工单质量检查编号 —— string GenerateWorkOrderInspectionNo()
        /// <summary>
        /// 生成工单质量检查编号
        /// </summary>
        /// <returns>工单质量检查编号</returns>
        public string GenerateWorkOrderInspectionNo()
        {
            return this._generator.Generate(typeof(WorkOrderInspection).FullName, "WOQI", DefaultTimeFormat, 3, "工单质量检查编号");
        }
        #endregion

        #region # 生成设备维护记录编号 —— string GenerateEquipmentMaintenanceNo()
        /// <summary>
        /// 生成设备维护记录编号
        /// </summary>
        /// <returns>设备维护记录编号</returns>
        public string GenerateEquipmentMaintenanceNo()
        {
            return this._generator.Generate(typeof(EquipmentMaintenance).FullName, "EM", DefaultTimeFormat, 3, "设备维护记录编号");
        }
        #endregion

        #region # 生成物料条码编号 —— string GenerateMaterialBarcodeNo()
        /// <summary>
        /// 生成物料条码编号
        /// </summary>
        /// <returns>物料条码编号</returns>
        public string GenerateMaterialBarcodeNo()
        {
            string timeFormat = "yyMMdd";
            string materialBarcodeNo = this._generator.Generate(typeof(MaterialBarcode).FullName, "M", timeFormat, 5, "物料条码编号");

            return materialBarcodeNo;
        }
        #endregion

        #region # 批量生成物料条码编号 —— string[] GenerateMaterialBarcodeNos(int count)
        /// <summary>
        /// 批量生成物料条码编号
        /// </summary>
        /// <param name="count">数量</param>
        /// <returns>物料条码编号列表</returns>
        public string[] GenerateMaterialBarcodeNos(int count)
        {
            string timeFormat = "yyMMdd";
            string[] materialBarcodeNos = this._generator.GenerateRange(typeof(MaterialBarcode).FullName, "M", timeFormat, 5, "物料条码编号", count);

            return materialBarcodeNos;
        }
        #endregion
    }
}
